package com.jl.js.console;

import java.io.File;
import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IOConsole;
import org.eclipse.ui.internal.console.ConsoleView;

import com.jl.card.Card;
import com.jl.card.CardManager;

public final class ConsoleUtil {

	private static HashMap<String, ConsoleShell> map = new HashMap<String, ConsoleShell>();

	/**
	 * get the current console shell object
	 * @return
	 */
	public static ConsoleShell getCurrentConsoleShell() {
		org.eclipse.ui.console.IConsole console = getCurrentConsole();
		if (console == null) {
			return null;
		}
		String name = console.getName();
		//console.
		ConsoleShell cs = map.get(name);
		if(cs == null){
			IConsoleManager consoleMgr = ConsolePlugin.getDefault().getConsoleManager();
			IConsole[] consoles = consoleMgr.getConsoles();
			Card lastCard = CardManager.getInstance().getLastConnectCard();
			if(lastCard == null){
				return null;
			}
			name = lastCard.getName();
			for(IConsole temp : consoles){
				if(temp.getName().equals(name)){
					consoleMgr.showConsoleView(temp);
				}
			}
			cs = map.get(name);
		}
		return cs;
	}
	
	@SuppressWarnings("restriction")
	public static IOConsole getCurrentConsole(){
		ConsoleView vp = (ConsoleView) PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage()
				.findView(IConsoleConstants.ID_CONSOLE_VIEW);
		if (vp == null) {
			return null;
		}
		IOConsole console = (IOConsole) vp.getConsole();
		if (console == null) {
			return null;
		}
		return console;
	}

	public static void register(String name, ConsoleShell shell) {
		if (map.containsKey(name)) {
			map.remove(name);
		}
		map.put(name, shell);
	}

	private final static char[] shiftChar = new char[] { '~', '!', '@', '#',
			'$', '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':',
			'"', '<', '>', '?' };

	public static void setFocus() {
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.findView(IConsoleConstants.ID_CONSOLE_VIEW).setFocus();
	}

	private static void preseKey(char ch) {
		Event evt = new Event();
		evt.type = SWT.KeyDown;
		evt.character = ch;
		Display.getDefault().post(evt);
	}

	private static void releaseKey(char ch) {
		Event evt = new Event();
		evt.type = SWT.KeyUp;
		evt.character = ch;
		Display.getDefault().post(evt);
	}

	private static void typeKey(char ch) {
		preseKey(ch);
		releaseKey(ch);
	}

	private static void preseKeyCode(int keycode) {
		Event evt = new Event();
		evt.type = SWT.KeyDown;
		evt.keyCode = keycode;
		Display.getDefault().post(evt);
	}

	private static void releaseKeyCode(int keycode) {
		Event evt = new Event();
		evt.type = SWT.KeyUp;
		evt.keyCode = keycode;
		Display.getDefault().post(evt);
	}

	private static void typeShiftChar(char ch) {
		preseKeyCode(SWT.SHIFT);
		typeKey(ch);
		releaseKeyCode(SWT.SHIFT);
	}

	private static boolean needPreseShift(char ch) {
		if (ch >= 'A' && ch <= 'Z') {
			return true;
		} else {
			for (char c : shiftChar) {
				if (c == ch) {
					return true;
				}
			}
		}
		return false;
	}

	public static String makeLoadCmd(File f) {
		String path = f.getAbsolutePath();
		path = path.replaceAll("\\\\", "/");
		return new StringBuilder("load('").append(path).append("' );")
				.toString();
	}
}
